Text會講三天,因為發現東西有點多,怕放在一篇會爆炸。
♠♣今天的文章大綱♥♦
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x100')
def cut():
text.event_generate("<<Cut>>")
def copy():
text.event_generate("<<Copy>>")
def paste():
text.event_generate("<<Paste>>")
def showPopupMenu(event):
popupmenu.post(event.x_root,event.y_root)
def undo():
try:
text.edit_undo()
except:
print("No action")
def redo():
try:
text.edit_redo()
except:
print("No action")
popupmenu=tk.Menu(root,tearoff=False)
popupmenu.add_command(label="cut",command=cut)
popupmenu.add_command(label="copy",command=copy)
popupmenu.add_command(label="paste",command=paste)
root.bind("<Button-3>",showPopupMenu)
toolbar=tk.Frame(root,relief="raised",borderwidth=1)
toolbar.pack(side="top",fill="x",padx=2,pady=1)
undoBtn = tk.Button(toolbar, text="Undo", command=undo)
undoBtn.pack(side="left", pady=2)
redoBtn = tk.Button (toolbar, text="Redo", command=redo)
redoBtn.pack(side="left", pady=2)
text = tk.Text (root, undo=True)
text.pack(fill="both", expand=True, padx=3, pady=2)
text.insert ('end', "我沒有說謊 我何必說謊\n")
text.insert ('end', "妳懂我的 我對妳從來就不會假裝 \n")
text.insert ('end', "我哪有說謊\n")
text.insert ('end', "請別以為妳有多難忘 笑是真的不是我逞強\n")
root.mainloop()
執行結果⬇⬇⬇
按undo就會還原空白,在按redo就會回復原來的樣子。
這個排版跟前面的不一樣喔~不知道大家有沒有發現呢?
這個範例適用grid做排版,我們在Day3、Day4跟Day5有講到grid跟pack不能同時出現在同一個程式裡面,一定要記得喔~不然會出錯~
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x100')
def cut():
text.event_generate("<<Cut>>")
def copy():
text.event_generate("<<Copy>>")
def paste():
text.event_generate("<<Paste>>")
def showPopupMenu(event):
popupmenu.post(event.x_root,event.y_root)
def undo():
try:
text.edit_undo()
except:
print("No action")
def redo():
try:
text.edit_redo()
except:
print("No action")
def search(): #搜尋開始
text.tag_remove("found","1.0","end")
start="1.0"
key=entry.get()
if(len(key.strip()) == 0):
return
while True:
pos=text.search(key,start,"end")
if (pos==""):
break
text.tag_add("found",pos,"%s+%dc" % (pos,len(key)))
start="%s+%dc" % (pos,len(key))
popupmenu=tk.Menu(root,tearoff=False)
popupmenu.add_command(label="cut",command=cut)
popupmenu.add_command(label="copy",command=copy)
popupmenu.add_command(label="paste",command=paste)
root.bind("<Button-3>",showPopupMenu)
root.rowconfigure(1,weight=1)
root.columnconfigure(0,weight=1)
entry=tk.Entry()
entry.grid(row=0,column=0, padx=5)
toolbar=tk.Frame(root,relief="raised",borderwidth=1)
toolbar.grid(row=0,column=4,padx=2,pady=1)
undoBtn = tk.Button(toolbar, text="Undo", command=undo)
undoBtn.grid(row=0,column=3, pady=5)
redoBtn = tk.Button (toolbar, text="Redo", command=redo)
redoBtn.grid(row=0,column=2, pady=5)
seabtn=tk.Button(root,text="Search",command=search)
seabtn.grid(row=0,column=1, padx=5,pady=5)
text = tk.Text (root, undo=True)
text.grid(row=1,column=0, padx=3, pady=2)
text.insert ('end', "我沒有說謊 我何必說謊\n")
text.insert ('end', "妳懂我的 我對妳從來就不會假裝 \n")
text.insert ('end', "我哪有說謊\n")
text.insert ('end', "請別以為妳有多難忘 笑是真的不是我逞強\n")
root.mainloop()
執行結果⬇⬇⬇
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x100')
def cut():
text.event_generate("<<Cut>>")
def copy():
text.event_generate("<<Copy>>")
def paste():
text.event_generate("<<Paste>>")
def showPopupMenu(event):
popupmenu.post(event.x_root,event.y_root)
def undo():
try:
text.edit_undo()
except:
print("No action")
def redo():
try:
text.edit_redo()
except:
print("No action")
def spelling():#拼字檢查開始
text.tag_remove("Error","1.0","end")
textwords=text.get("1.0","end").split()
print("Dictionary")
startChar=("(")
endChar=(".",",","?",")","!",":",";")
start="1.0"
for word in textwords:
if word[0] in startChar:
word=word[1:]
if word[-1] in startChar:
word=word[:-1]
if (word not in dicts and word.lower()not in dicts):
print("error",word)
pos=text.search(word,start,"end")
text.tag_add("spellErr",pos,"%s+%dc" % (pos,len(word)))
pos="%s+%dc" % (pos,len(word))
def clr():
text.tag_remove("spellErr","1.0","end")
popupmenu=tk.Menu(root,tearoff=False)
popupmenu.add_command(label="cut",command=cut)
popupmenu.add_command(label="copy",command=copy)
popupmenu.add_command(label="paste",command=paste)
root.bind("<Button-3>",showPopupMenu)
toolbar=tk.Frame(root,relief="raised",borderwidth=1)
toolbar.pack(side="top",fill="x",padx=2,pady=1)
chkBtn=tk.Button(toolbar,text="check",command=spelling)
chkBtn.pack(side="left", pady=2)
clrBtn=tk.Button(toolbar,text="clear",command=clr)
clrBtn.pack(side="left", pady=2)
undoBtn = tk.Button(toolbar, text="Undo", command=undo)
undoBtn.pack(side="left", pady=2)
redoBtn = tk.Button (toolbar, text="Redo", command=redo)
redoBtn.pack(side="left", pady=2)
text = tk.Text (root, undo=True)
text.pack(fill="both", expand=True, padx=3, pady=2)
text.insert ('end', "我沒有說謊 我何必說謊\n")
text.insert ('end', "妳懂我的 我對妳從來就不會假裝 \n")
text.insert ('end', "我哪有說謊\n")
text.insert ('end', "請別以為妳有多難忘 笑是真的不是我逞強\n")
text.tag_configure("spellerror",foreground="red")
with open("myDict.txt","r") as dictObj: #自己建立一個記事本在同一個資料夾裡當作字典
dicts=dictObj.read().split("\n")
root.mainloop()
執行結果⬇⬇⬇
今天的這篇有做不一樣的嘗試,就是後一個範例會結合前面的一起呈現,所以程式碼比較長,但是有標記從哪裡開始是那個範例的主題,因為覺得如果只單做一個太無聊了,所以就決定用結合的方式作呈現,也可以知道之後真正用在介面上的感覺。
最後祝台灣生日快樂啦~~~